package com.hc.appanimal;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hc.DTO.AnimalDTO;
import com.hc.DTO.AnimalImagesDTO;
import com.hc.DTO.TokenDTO;
import com.hc.constants.Constants;
import com.hc.model.utils.AnimalImagesModel;
import com.hc.model.utils.AnimalModel;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.AsyncTask;

public class AsystaskDeleteAnimal extends AsyncTask<Void, Void, Void> {

	Context mContext;
	Dialog dialog;
	AnimationDrawable frameAnimation;
	TokenDTO token;

	public AsystaskDeleteAnimal(Context context, TokenDTO _token) {
		mContext = context;
		token = _token;
		dialog = new Dialog(mContext, R.style.NewDialog);
		dialog.setContentView(R.layout.loading);

		dialog.setCancelable(false);
		dialog.findViewById(R.id.loading_icon).setBackgroundResource(
				R.drawable.spin_animation);
		frameAnimation = (AnimationDrawable) dialog.findViewById(
				R.id.loading_icon).getBackground();
		dialog.show();
		frameAnimation.start();
	}

	@Override
	protected Void doInBackground(Void... params) {
		HttpEntity httpEntity = null;
		HttpClient client = new DefaultHttpClient();
		HttpGet method = new HttpGet(Constants.URL_DELETE_ANIMAL);
		method.setHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=UTF-8");
		method.setHeader("Authorization",
				token.getToken_type() + " " + token.getAccess_token());
		UrlEncodedFormEntity formEntity = null;
		try {
			// formEntity = new UrlEncodedFormEntity(postParams);
			// method.SET

			HttpResponse response = client.execute(method);
			httpEntity = response.getEntity();
			String strJson = EntityUtils.toString(httpEntity);
			if (strJson != null) {
				JSONArray jsonArr = new JSONArray(strJson);
				JSONObject jsonAnimals = jsonArr.getJSONObject(0);
				JSONObject jsonAnimalImages = jsonArr.getJSONObject(1);

				JSONArray arrIdSAnimals = jsonAnimals.getJSONArray("Ids");
				JSONArray arrIdSAnimalImages = jsonAnimalImages
						.getJSONArray("Ids");

				for (int j = 0; j < arrIdSAnimals.length(); j++) {
					int number = arrIdSAnimals.getInt(j);
					AnimalDTO dtoDL = new AnimalDTO();
					dtoDL.setArrIdRemove(number);
					AnimalModel modelDelete = AnimalModel.getInstance();
					modelDelete.deleteAnimal(dtoDL, mContext);

				}
				for (int k = 0; k < arrIdSAnimalImages.length(); k++) {
					AnimalImagesDTO dtoImgDLT = new AnimalImagesDTO();
					int numberIMG = arrIdSAnimalImages.getInt(k);
					dtoImgDLT.setIdImgeDelete(numberIMG);
					AnimalImagesModel modelDeleteIMG = AnimalImagesModel
							.getInstance();
					modelDeleteIMG.deleteAnimalIMG(dtoImgDLT, mContext);

				}

			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		dialog.dismiss();
		frameAnimation.stop();
	}

}
